package com.borball.store.comp;
import java.awt.BorderLayout;
import java.sql.Timestamp;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableRowSorter;

import com.borball.store.dao.OrderDAO;
import com.borball.store.dao.OrderItemDAO;
import com.borball.store.domain.PStoreLog;
import com.borball.store.domain.ProductAmount;

import craky.component.CommonTableModel;
import craky.componentc.JCScrollTable;
import craky.componentc.JCTable;
import craky.componentc.JCTableHeader;
import craky.keeper.EmptyComponent;
import craky.keeper.KeeperPane;

public class SaleReportPane extends KeeperPane {

	private static final long serialVersionUID = 2583431180094797757L;

	protected JCTable table1;
	protected JCTable table2;
	protected List<ProductAmount> data1;
	protected List<ProductAmount> data2;
	protected CommonTableModel<ProductAmount> model1;
	protected CommonTableModel<ProductAmount> model2;
	private static final String[] COLUMNS_NAME = { "名称", "数量" };
	protected JComponent bottom, center;
	private OrderDAO orderDao = new OrderDAO();
	private OrderItemDAO orderItemDao = new OrderItemDAO();

	public SaleReportPane() {
		loadData();
		initUI();
	}


	private void loadData() {
		data1 = orderItemDao.orderByAmount(null);
		data2 = orderDao.orderByAmount(null);
	}

	private void initUI() {
//		btnInsert = new JCButton(insertAction);
//		btnInsert.setPreferredSize(new Dimension(75, 30));
		EmptyComponent centerParent = new EmptyComponent();
		setLayout(new BorderLayout());
		centerParent.setLayout(new BorderLayout(0, 3));
		createCenter1();
		createCenter2();
//		createBottom();

		this.add(centerParent, BorderLayout.CENTER);

		if (center != null) {
			centerParent.add(center, BorderLayout.CENTER);
		}

//		if (bottom != null) {
//			centerParent.add(bottom, BorderLayout.NORTH);
//		}
		
//		loadTypes();
//		loadUsers();
//		loadUnits();

	}


	@Override
	public String getType() {
		return "成品库存变更";
	}

	protected JComponent createCenter1() {
		final Class<?>[] columnsClass = { String.class, Integer.class};
		final String[] getMethodsName = { "getName", "getAmount" };
		final String[] setMethodsName = { "setName", "setAmount" };
		final int[] columnsPreferredWidth = { 40, 60 };
		EmptyComponent ec = new EmptyComponent();
		JCScrollTable scTable = new JCScrollTable();
		table1 = scTable.getTable();
		model1 = new CommonTableModel<ProductAmount>(table1, COLUMNS_NAME, columnsClass, getMethodsName, setMethodsName,
				data1);
		TableRowSorter<CommonTableModel<ProductAmount>> sorter = new TableRowSorter<CommonTableModel<ProductAmount>>(
				model1);

		scTable.setAlpha(0.0f);
		scTable.setColumnControlEnabled(false);

		ec.setLayout(new BorderLayout());
		ec.add(scTable, BorderLayout.CENTER);
		table1.setModel(model1);
		table1.setDragEnabled(false);
		table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table1.setRowSorter(sorter);
		table1.setRendererOpaque(false);
		((JCTableHeader) table1.getTableHeader()).setShowPopupMenu(false);

		for (int i = 0; i < columnsPreferredWidth.length; i++) {
			table1.getColumnModel().getColumn(i).setPreferredWidth(columnsPreferredWidth[i]);
		}

		return center = ec;
	}

	
	protected JComponent createCenter2() {
		final Class<?>[] columnsClass = { String.class, Integer.class};
		final String[] getMethodsName = { "getName", "getAmount" };
		final String[] setMethodsName = { "setName", "setAmount" };
		final int[] columnsPreferredWidth = { 40, 60 };
		EmptyComponent ec = new EmptyComponent();
		JCScrollTable scTable = new JCScrollTable();
		table2 = scTable.getTable();
		model2 = new CommonTableModel<ProductAmount>(table2, COLUMNS_NAME, columnsClass, getMethodsName, setMethodsName,
				data2);
		TableRowSorter<CommonTableModel<ProductAmount>> sorter = new TableRowSorter<CommonTableModel<ProductAmount>>(
				model2);

		scTable.setAlpha(0.0f);
		scTable.setColumnControlEnabled(false);

		ec.setLayout(new BorderLayout());
		ec.add(scTable, BorderLayout.CENTER);
		table2.setModel(model2);
		table2.setDragEnabled(false);
		table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table2.setRowSorter(sorter);
		table2.setRendererOpaque(false);
		((JCTableHeader) table2.getTableHeader()).setShowPopupMenu(false);

		for (int i = 0; i < columnsPreferredWidth.length; i++) {
			table2.getColumnModel().getColumn(i).setPreferredWidth(columnsPreferredWidth[i]);
		}

		return center = ec;
	}
	

	@Override
	public void refresh() {
		loadData();
		model.delAllRow();
		model.setDataList(data);
		model.getTable().repaint();
		model.refreshRowOnView(0);
	}

}
